package com.kzw.config;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.resource.ResourceUrlProvider;

/**
 * 使用ResourceUrlProvider进行版本管理 并避免在版本发生改变时，由于浏览器缓存而产生资源版本未改变的错误
 * 应用到所有@RequestMapping中，可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute
 */
@ControllerAdvice
public class ControllerConfig {

	@Autowired
	private ResourceUrlProvider resourceUrlProvider;

	@ModelAttribute("urls")
	public ResourceUrlProvider urls() {
		return this.resourceUrlProvider;
	}
	
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
	    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));        
	}
}
